package wholemusic.web.watch;

import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class CompressUtils {
    public static byte[] compress(byte[] input, int maxSize) {
        // Compress the bytes
        byte[] output = new byte[maxSize];
        Deflater compressor = new Deflater();
        compressor.setInput(input);
        compressor.finish();
        int compressedDataLength = compressor.deflate(output);
        byte[] result = new byte[compressedDataLength];
        System.arraycopy(output, 0, result, 0, compressedDataLength);
        return result;
    }

    public static byte[] decompress(byte[] input, int maxSize) {
        try {
            // Decompress the bytes
            Inflater decompressor = new Inflater();
            decompressor.setInput(input);
            byte[] output = new byte[maxSize];
            int resultLength = decompressor.inflate(output);
            decompressor.end();
            byte[] result = new byte[resultLength];
            System.arraycopy(output, 0, result, 0, resultLength);
            return result;
        } catch (DataFormatException ex) {
            return null;
        }
    }
}
